<?php

namespace app\admin\controller;

use think\Db;
use think\Controller;
use think\Request;
use think\Session;

class Movie extends AdminController
{
    public function _initialize()
    {
        parent::_initialize();
    }
    // public function index()
    // {
    //     return view('admin@con1/tables');
    // }

    public function index()
    {
        // 读取内容列表
        $res =Db::table('sm_h_movie')
            ->alias('shm')
            ->join('sm_h_am sham', 'sham.mid  = shm.id')
            ->join('sm_h_actor sha', 'sha.id = sham.aid')
            ->field('shm.mname,sha.name,shm.id')
            ->select();
            // var_dump($res);die;
            // ->paginate(5);
        $data = Db::table('sm_h_movie')->order('id','desc')->paginate(5);
        $page = $data->render();
        return view('admin@Movie/index',[
            'data' => $data,
            'res' => $res,
            'page' => $page
        ]);

    }

    public function add()
    {
        return view('admin@Movie/add', [
            'title' => '内容添加'
        ]);
    }

    // 添加内容
    public function insert(Request $request)
    {
        $data = $request->post();
        $file = request()->file('cover');
        if (empty($file)) {
            $this->error('请添加封面 ', url('admin/Movie/add'));
        }
        $d['mname'] = $data['mname'];
        $d['country'] = $data['country'];
        $d['describe'] = $data['describe'];
        $d['myear'] = $data['myear'];
        $d['mtime'] = $data['mtime'];
        $d['director'] = $data['director'];
        $d['type'] = $data['type'];

        if (isset($file)) {
            // 获取表单上传文件 例如上传了001.jpg
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public/uploads');
//       var_dump($info) ;die;

            if ($info) {
                // 成功上传后 获取上传信息
                $a = $info->getSaveName();
                $cover = str_replace("\\", "/", $a);

                $s = [
                    'mname' => $d['mname'],
                    'country' => $d['country'],
                    'describe' => $d['describe'],
                    'myear' => $d['myear'],
                    'mtime' => $d['mtime'],
                    'director' => $d['director'],
                    'cover' => $cover,
                    'type' => $d['type']
                ];
                $result = Db('h_movie')->insert($s);
                $mid = Db::name('h_movie')->field('id')->where('mname', $d['mname'])->find();
                $d = [
                    'mid' => $mid['id'],
                    'aid' => '3'
                ];
                $res = Db('h_am')->insert($d);
                if ($result && $res) {
                    return $this->success('新增成功', 'Movie/index');
                } else {
                    return $this->error('新增失败');
                }

            } else {
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }


//    var_dump($data);die;


    }

    public function delete($id)
    {
//         echo $id;
        $result = db('h_movie')->delete($id);
        if ($result > 0) {
            $info['status'] = true;
        } else {
            $info['status'] = false;
        }
        return json($info);

    }

    public function edit($id)
    {
        $data = db('h_movie')->find($id);
        $imgs = $data['cover'];
        return view('admin@Movie/edit', [
            'title' => '我是修改ID:' . $id . '的内容页面',
            'data' => $data,
            'id' => $id,
            'imgs' => $imgs
        ]);
    }

    public function update(Request $request)
    {
        $data = $request->post();

        $file = request()->file('cover');
        $id = $data['id'];

        if (isset($file)) {

            // 获取表单上传文件 例如上传了001.jpg
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public/uploads');
//       var_dump($info) ;die;

            if ($info) {
                // 成功上传后 获取上传信息
                $a = $info->getSaveName();
                $cover = str_replace("\\", "/", $a);

                $a = [
                    'mname' => $data['mname'],
                    'country' => $data['country'],
                    'describe' => $data['describe'],
                    'myear' => $data['myear'],
                    'mtime' => $data['mtime'],
                    'director' => $data['director'],
                    'cover' => $cover,
                    'type' => $data['type']
                ];

                $result = Db('h_movie')->where('id', $id)->update($a);

                if ($result) {
                    return $this->success('编辑成功', 'Movie/index');
                } else {
                    return $this->error('编辑失败');
                }

            }
        } elseif (empty($file)) {
            $a = [
                'mname' => $data['mname'],
                'country' => $data['country'],
                'describe' => $data['describe'],
                'myear' => $data['myear'],
                'mtime' => $data['mtime'],
                'director' => $data['director'],
                'type' => $data['type']
            ];

            $result = Db('h_movie')->where('id', $id)->update($a);

            if ($result) {
                return $this->success('编辑成功', 'Movie/index');
            } else {
                return $this->error('编辑失败');
            }

        } else {

            // 上传失败获取错误信息
            echo $file->getError();
        }


    }

    // 分配角色页面
    public function getActor($id)
    {

        $input = input();
        $mid = $input['id'];
//         var_dump($mid);die;
        $list = Db::table('sm_h_movie')
            ->alias('shm')
            ->join('sm_h_am sham', 'sham.mid = shm.id')
            ->join('sm_h_actor sha', 'sha.id = sham.aid')
            ->field('sha.name as shaname,sha.id as shaid')
            ->where('shm.id', $mid)
            ->select();

//         dump($list);
        $aname = $list;
        $actor = Db::name('h_actor')->field(['id', 'name',])->order('id', 'ASC')->select();

        foreach ($aname as $v) {
            $myactor[] = $v['shaid'];
        }
//        dump($actor);
//        dump($myactor);die;

        return view('admin@Movie/chooseActor', [
            'actor' => $actor,
            'myactor' => $myactor,
            'mid' => $mid,
            'title' => '我是ID:' . $mid . '电影分配演员页面'
        ]);

    }

    public function updateActor(Request $request)
    {
        $data = $request->post();
        // var_dump($data);
        $id = $data['mid'];
        // 删除原来角色
        $r = Db::name('h_am')->where('mid', $id)->delete();


        foreach ($data['actor'] as $k => $v) {
            $a = [
                'aid' => $v,
                'mid' => $id
            ];
            $result = Db::name('h_am')->where('mid', $id)->insert($a);
        }


        if ($result) {
            return $this->success('分配演员成功', 'Movie/index');
        } else {
            return $this->error('分配演员失败');
        }
    }


}
